namespace CGF
{
    public partial class PanelDeviceDetail : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        private string income, botId;

        public static void Open(string botId, string income)
        {
            UIManagerComponent.instance.OpenUI<PanelDeviceDetail>(UILayer.Normal, RootComponent.instance, false).ReqData(botId, income);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelDeviceDetail>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelDeviceDetail", layer, isFloat);
            BindProperties(transform);

            BtnBack.SetClick(Close);

            Title.text = "";

            if (botId != null)
            {
                ReqData(botId, income);
            }
        }

        private void ReqData(string botId, string income)
        {
            this.botId = botId;
            this.income = income;
            if (!Title) return;

            Title.text = $"{I18N_device_code}: {botId}";
            TextIncome.text = $"{I18N_total_income}: {I18N.Currency} {income}";

            //表格
            string api = "/v1/earnings/bot/" + botId;
            string listKey = "Results";
            Table.Column[] columns =
            {
                (cell, data) => TimeHelper.Parse2Date(data["created_at"].ToString(), false),
                (cell, data) => TimeHelper.Parse2Time(data["created_at"].ToString(), false),
                (cell, data) => I18N_by_sell_products,
                (cell, data) => $"<color={Config.NumColor}>{I18N.Currency}<b>{StringHelper.Format2Decimal((int)data["amount"] * 0.01f)}</b></color>",
            };
            Table.GetComponent<Table>().ReqData(api, listKey, columns);
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

    }
}
